//
//  DiagnosisSoundViewController.m
//  MobileChecker
//
//  Created by Lymons on 15-1-12.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "DiagnosisSoundViewController.h"
#import "SelfDiagnosisViewModel.h"
#import "HUDHelper.h"

@interface DiagnosisSoundViewController ()

@property (strong, nonatomic) IBOutlet UIButton *resultOkButton;
@property (strong, nonatomic) IBOutlet UIButton *resultNgButton;
@property (strong, nonatomic) IBOutlet UIButton *playRecordButton;
@property (strong, nonatomic) IBOutlet UIButton *playSoundButton;

@property (strong, nonatomic) DiagnosisSoundViewModel *viewModelPlayMedia;
@property (strong, nonatomic) DiagnosisSoundViewModel *viewModelPlayRecord;

@property (assign, nonatomic) BOOL playRecordPressed;
@property (assign, nonatomic) BOOL playSoundPressed;
@property (assign, nonatomic) BOOL hasRecodeFile;

@end

@implementation DiagnosisSoundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self bindViewModel];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.viewModelPlayMedia completeTheCheckCommand];
    [self.viewModelPlayRecord completeTheCheckCommand];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -

- (void)bindViewModel
{
    self.viewModelPlayMedia = [[DiagnosisSoundViewModel alloc] initWithType:SoundDiagnosisServiceTypeMedia];
    self.viewModelPlayRecord = [[DiagnosisSoundViewModel alloc] initWithType:SoundDiagnosisServiceTypeRecord];
    
    float yOffsetOfToast = CGRectGetHeight([[UIScreen mainScreen] bounds]) - 120;
    [RACObserve(self.viewModelPlayRecord, toastMessage) subscribeNext:^(id x) {
        if (x) {
            [[HUDHelper sharedInstance] showToastAlways:nil message:x yOffset:yOffsetOfToast];
        } else {
            [[HUDHelper sharedInstance] hideHUD];
        }
    }];
    
    [RACObserve(self.viewModelPlayMedia, toastMessage) subscribeNext:^(id x) {
        if (x) {
            [[HUDHelper sharedInstance] showToastAlways:nil message:x yOffset:yOffsetOfToast];
        } else {
            [[HUDHelper sharedInstance] hideHUD];
        }
    }];
    
    @weakify(self);
    [[self.viewModelPlayRecord requestRecordPermission] subscribeNext:^(id x) {
        @strongify(self);
        BOOL granted = ((NSNumber *)x).boolValue;
        self.hasRecodeFile = granted;
        if (!granted) {
            self.playRecordPressed = YES;
        }
    }];
    
    self.playRecordButton.rac_command = self.viewModelPlayRecord.executeCheck;
    self.playSoundButton.rac_command = self.viewModelPlayMedia.executeCheck;
    
//    [[RACSignal combineLatest:@[
//                                [self.playRecordButton.rac_command.executing not],
//                                [self.playSoundButton.rac_command.executing not],
//                                RACObserve(self, hasRecodeFile)]
//                       reduce:^(NSNumber *executing1, NSNumber *executing2, NSNumber *hasRecodeFile){
//                                    return @([executing1 boolValue] && [executing2 boolValue]);
//                                }] subscribeNext:^(NSNumber *x) {
//                                    @strongify(self)
//                                    self.playSoundButton.enabled = [x boolValue];
//                                    self.playRecordButton.enabled = [x boolValue] && self.hasRecodeFile;
//                                }];
    
    void (^resultBlock)(BOOL) = ^(BOOL result) {
        NSLog(@"Speaker is OK : %@", result ? @"YES" : @"NO");
        @strongify(self);
        [self performSegueWithIdentifier:@"SoundPlayCompleted" sender:self];
    };
    self.resultOkButton.rac_command = [self.viewModelPlayMedia makeCommandWithBlock:resultBlock simple:YES];
    self.resultNgButton.rac_command = [self.viewModelPlayMedia makeCommandWithBlock:resultBlock simple:NO];
    [self.playRecordButton.rac_command.executionSignals subscribeNext:^(id x) {
        self.playRecordPressed = YES;
        self.playRecordButton.enabled = NO;
    }];
    [self.playSoundButton.rac_command.executionSignals subscribeNext:^(id x) {
        self.playSoundPressed = YES;
        self.playSoundButton.enabled = NO;
    }];
    
    self.playSoundButton.enabled = NO;
    [RACObserve(self, viewModelPlayRecord.checkCompleted) subscribeNext:^(id x) {
        @strongify(self);
        if ([x boolValue]) {
            self.playSoundButton.enabled = YES;
            [self performSelector:@selector(disableRecordButton) withObject:nil afterDelay:0.3];
//            self.playRecordButton.enabled = NO;
        }
    }];
    [RACObserve(self, viewModelPlayMedia.checkCompleted) subscribeNext:^(id x) {
        @strongify(self);
        if ([x boolValue]) {
            self.playRecordButton.enabled = YES;
            [self performSelector:@selector(disableSoundButton) withObject:nil afterDelay:0.3];
//            self.playSoundButton.enabled = NO;
        }
    }];
    
    RAC(self, resultOkButton.enabled) = [RACSignal
                                         combineLatest:@[RACObserve(self, playSoundPressed), RACObserve(self, playRecordPressed)]
                                         reduce:^(NSNumber *execute1, NSNumber *execute2) {
                                             return @([execute1 boolValue] && [execute2 boolValue]);
                                         }];
    RAC(self, resultNgButton.enabled) = RACObserve(self.resultOkButton, enabled);
}

- (void)disableRecordButton
{
    self.playRecordButton.enabled = NO;
}

- (void)disableSoundButton
{
    self.playSoundButton.enabled = NO;
}

@end
